//
//  APIWorkOutRank.m
//  Bracelet
//
//  Created by jing zhao on 2/15/15.
//  Copyright (c) 2015 jing zhao. All rights reserved.
//

#import "APIWorkOutRank.h"

@implementation APIWorkOutRank


-(instancetype) initFromRankData:(NSDictionary*)dic
{
    self = [super init];
    if (self) {
        NSArray *arrayRank = [dic objectForKey:APIKEYALLRANK];
        NSDictionary *myRank = [dic objectForKey:APIKEYMYRANK];
        NSDictionary *paper = [dic objectForKey:APIKEYPAPER];
        
        _paper = [APIPaper apiFromDictionaryPaper:paper];
        _myRank = [APIOneRank apiFromDictionaryRankOnePeople:myRank];
        _totalRunner = [dic objectForKey:APIKEYTOTALRUNNERS];
        _arrayAllRank = [[NSMutableArray alloc]init];
        for (NSDictionary* dicRankOne in arrayRank) {
             APIOneRank *paper =  [APIOneRank apiFromDictionaryRankOnePeople:dicRankOne];
            [_arrayAllRank addObject:paper];
        }
    }
    return self;
}


+(instancetype)apiRankWithData:(NSDictionary*)dic
{
   return  [[self alloc]initFromRankData:dic];
}

+(void)setApiRelativePosition:(NSString*)relativePostion
                    withPaper:(NSString*)paperNu
              getTheTotalRank:(void (^)(APIWorkOutRank *outRank))successBlock
                      failure:(void (^)( NSError *error)) failureBlock
{
    NSString *strUserId = [[NSUserDefaults standardUserDefaults]objectForKey:USERID];
    NSDictionary *headerData =@{@"BasicAuthUsername":strUserId};
    NSDictionary *data = @{@"flag":@"all",@"pager.pageNo":paperNu};
    
    [[APIConnection shareInstance]post:relativePostion data:data header:headerData netError:^(NSError *error) {
        failureBlock(error);
    } apiFinish:^(id data) {
        NSDictionary *dictionaryTemp = data;
        NSInteger codeTemp= [[dictionaryTemp objectForKey:APIKEYCODE]integerValue];
        if (codeTemp ==0) {
            NSDictionary *dicSuccess = [data objectForKey:APIKEYData];
            successBlock([APIWorkOutRank apiRankWithData:dicSuccess]);
        }
        else
        {
            failureBlock(nil);
        }
    }];
}

+(void)setApiRelativePosition:(NSString*)relativePostion
                    withPaper:(NSString*)paperNu
               getTheWeekRank:(void (^)(APIWorkOutRank *outRank))successBlock
                      failure:(void (^)( NSError *error)) failureBlock
{

    NSString *strUserId = [[NSUserDefaults standardUserDefaults]objectForKey:USERID];
    NSDictionary *headerData =@{@"BasicAuthUsername":strUserId};
    NSDictionary *data = @{@"flag":@"week",@"pager.pageNo":paperNu};
    
    [[APIConnection shareInstance]post:relativePostion data:data header:headerData netError:^(NSError *error) {
        failureBlock(error);
    } apiFinish:^(id data) {
        NSDictionary *dictionaryTemp = data;
        NSInteger codeTemp= [[dictionaryTemp objectForKey:APIKEYCODE]integerValue];
        if (codeTemp ==0) {
            NSDictionary *dicSuccess = [data objectForKey:APIKEYData];
            successBlock([APIWorkOutRank apiRankWithData:dicSuccess]);
        }
        else
        {
            failureBlock(nil);
        }
    }];
}

@end


